#  Logtrack-ng - Module for dealing with exceptions in syslog
#  Copyright (C) 2007 Jan Kucera
# 
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
# 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
#  USA


package Logtrack::Syslog::Exceptions;

use lib "$ENV{PWD}/modules";
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(&repeated_msg &end_session);

=head1 NAME

Logtrack::Syslog::Login - modul pro zpracovani vyjimek v syslogu

=head1 DESCRIPTION

Modul zpracovava opakovani zprav, kdy pricte hodnotu k dane udalosti

=head2 FUNCTIONS

=over 4

=item repeated_msg

=cut

use strict;
use Data::Dumper;
use Logtrack::Syslog::Login;
$Data::Dumper::Indent = 1;

our @EXPORT_OK;


=item repeated_msg
Funkce repeated_msg zpracuje svoji vstupni zpravu a extrahuje z ni pocet
opakovani a rovnez jmeno serveru, z ktereho zprava dosla. Pote dohleda v
databazi zaznam v tabule events. Pokud v atributu id_event nalezne ID udalosti
automaticky k ni pripocte pocet opakovani.
=cut


sub repeated_msg {
	my ($ref_logtrack,$ref_line,$ref_config,$count) = @_;
	my $ref_db = $$ref_logtrack->{dbi}->get_db();
	my (%message,$query,$result,$service_key,@keys,@array,@dirty_incident);
	
	$$ref_logtrack->debug("Spustena vyjimka Repeated_msg\n");
	
	$$ref_logtrack->parse_msg($ref_line,
		\$$ref_config->{logtype}->[0],\%message);
	$$ref_logtrack->parse_msg($ref_line,
		\$$ref_config->{logtype}->[1],\%message);
	
	return if(not defined $$ref_config->{keys});
	@keys = split /\|/ , $$ref_config->{keys};
	
	foreach my $key (@keys) {
		$array[$key] = $message{$$ref_config->{logtype}->[$key]->{name}};
	}
	
	foreach my $prvek (@array) {
		next if(not $prvek);
		if($service_key) {
			$service_key .= '|' . $prvek;
		}
		else {
			$service_key = $$ref_config->{file} .": ". $prvek;
		}
	}
	print $service_key;
	$result = $$ref_db->selectrow_hashref('SELECT * FROM services 
		WHERE key = ?', {}, $service_key);
	
	return if(not defined $result);
	
	#Udaj je prazdny - tykal se zpravy, ktera by odfiltrovana
	return if(not $result->{id_event}); 
	
	$$ref_db->commit if($$ref_db->do('UPDATE events 
		SET count = count + ? WHERE id = ?',{},$count,$result->{id_event}));
	
#	This code is deprecated - function require more arguments


# 	$query = $$ref_db->prepare('SELECT id_incident 
# 		FROM events_incidents WHERE id_event = ?');
# 	$query->execute($result->{id_event});
# 	return if(not $query->rows);
# 	
# 	while($result = $query->fetchrow_hashref) {
# 		push @dirty_incident,$result->{id_incident};
# 	}
# 	
# 	Logtrack::Syslog::Login::check($ref_logtrack,@dirty_incident);
	return;
}

1;
